package com.schedule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.schedule.mapper.CompetitionMapper;
import com.schedule.pojo.Competition;
import com.schedule.pojo.Judge;
import com.schedule.pojo.RespBean;
import com.schedule.pojo.ShowCompetitionInfo;
import com.schedule.service.ICompetitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "赛事管理")
@RestController      //@Controller不适合返回json内容
@Slf4j
public class CompetitionController {

//    @Autowired
//    private CTypeMapper cTypeMapper;

    @Autowired     //寻找IOC容器里含有的同类型对象进行装配
    private CompetitionMapper competitionMapper;           //这里的“无法自动装配”报错不影响使用

    @Autowired
    private ICompetitionService competitionService;


    @ApiOperation("创建一项比赛")
    @PostMapping("/competition/create/addCompetition")
    public RespBean addCompetition(@RequestBody Competition competition){
//        for (Competition competition : competitions) {
        competitionMapper.insert(competition);
//        }

        return RespBean.success("添加成功");
    }

    //显示比赛信息和裁判姓名，涉及联立查表

    @ApiOperation("分页获取全部比赛")
    @GetMapping("/getAll")
    public RespBean getAll(@RequestParam("PageNum") int PageNum, @RequestParam("PageSize")int PageSize){
        Page<ShowCompetitionInfo> competitionInfoPage=new Page<>(PageNum,PageSize);
        //https://gitee.com/best_handsome/mybatis-plus-join
        IPage<ShowCompetitionInfo> showCompetitionInfos=competitionMapper.selectJoinPage(competitionInfoPage,ShowCompetitionInfo.class,
                new MPJLambdaWrapper<Competition>()
                        //只从Competition表里面去下面的字段，因为这些字段名和结果展示类的字段名一样，所以不需要selectAs方法
                        .select(Competition::getCName,Competition::getCRule,Competition::getCRule,Competition::getTimeNeed)
                        //指名Judge表里面的Name字段对应结果展示类的HeadJudgeName字段
                        .selectAs(Judge::getName,ShowCompetitionInfo::getHeadJudgeName)
                        //连表查询条件，两个参数相等
                        .leftJoin(Judge.class,Judge::getJId,Competition::getHeadJudge));
        List<ShowCompetitionInfo> showCompetitionInfoList=showCompetitionInfos.getRecords();
//        System.out.println("总共"+showCompetitionInfos.getTotal());
//        System.out.println("页数"+showCompetitionInfos.getPages());
        if(showCompetitionInfoList.size()!=0)  return RespBean.success(String.valueOf(showCompetitionInfos.getTotal()),showCompetitionInfoList);
        else return RespBean.error("没有查询到信息");
    }

    //通过名字模糊查询
    @ApiOperation("名字模糊查询")
    @PostMapping("/query")
    public RespBean queryCompetitions(@RequestParam("word") String word){
//        System.out.println("获取到的word为 "+word);
        List<ShowCompetitionInfo> showCompetitionInfos=competitionMapper.selectJoinList(ShowCompetitionInfo.class,
                new MPJLambdaWrapper<Competition>()
                        //只从Competition表里面去下面的字段，因为这些字段名和结果展示类的字段名一样，所以不需要selectAs方法
                        .select(Competition::getCName,Competition::getCRule,Competition::getCRule,Competition::getTimeNeed)
                        //指名Judge表里面的Name字段对应结果展示类的HeadJudgeName字段
                        .selectAs(Judge::getName,ShowCompetitionInfo::getHeadJudgeName)
                        //连表查询条件，两个参数相等
                        .leftJoin(Judge.class,Judge::getJId,Competition::getHeadJudge)
                        //查询条件
                        .like(Competition::getCName,word));
        if(showCompetitionInfos.size()!=0)  return RespBean.success("查询成功",showCompetitionInfos);
        else return RespBean.error("没有查询到信息");
    }

    @ApiOperation("删除比赛信息")
    @PostMapping("/competition/edit/deleteCompetition")
    public RespBean deleteCompetition(String CName){
        int Num=competitionMapper.deleteById(CName);
        if(Num==0) return RespBean.error("比赛信息不存在，删除失败");
        return RespBean.success("删除成功");
    }

    @ApiOperation("更新比赛信息")
    @PostMapping("/competition/edit/UpdateCompetition")
    public RespBean updateCompetition(@RequestBody Competition competition){
        System.out.println("competition is "+competition);
        int Num=competitionMapper.updateById(competition);
        if(Num==0) return RespBean.error("比赛信息不存在，更新失败");
        return RespBean.success("更新成功",competition);
    }

//    //通过比赛编号进行查询
//    @PostMapping("/queryById")
//    public ShowCompetitionInfo queryByIdCompetition(String CId){
//        return competitionMapper.selectJoinOne(ShowCompetitionInfo.class,
//                new MPJLambdaWrapper<Competition>()
//                        //只从Competition表里面去下面的字段，因为这些字段名和结果展示类的字段名一样，所以不需要selectAs方法
//                        .select(Competition::getCId,Competition::getCName,Competition::getCRule,Competition::getCRule,Competition::getPlace,Competition::getTimeNeed)
//                        //指名Judge表里面的Name字段对应结果展示类的HeadJudgeName字段
//                        .selectAs(Judge::getName,ShowCompetitionInfo::getHeadJudgeName)
//                        //连表查询条件，两个参数相等
//                        .leftJoin(Judge.class,Judge::getJId,Competition::getHeadJudge)
//                        .eq(Competition::getCId,CId));   //likeRight：'值%'
//    }

//    //通过类型进行查询
//    @PostMapping("/queryByType")
//    //instrument和size不同的值表示不做选择、要器械和不要器械
//    public List<ShowCompetitionInfo> queryByTypeCompetitions(String type, int instrument, int size){
//        String code="";
//
//        String typeCode=getType(type);
//        if("".equals(typeCode)) {
//            code+="__";         //表示类型的两位编号可以随意
//        }else{
//            code+=typeCode;
//        }
//
//        if(instrument==0)  code+="_";
//        else if(instrument==1)  code+="1";
//        else {
//            code+="00";         //不使用器械
//            return competitionMapper.selectJoinList(ShowCompetitionInfo.class,
//                    new MPJLambdaWrapper<Competition>()
//                            //只从Competition表里面去下面的字段，因为这些字段名和结果展示类的字段名一样，所以不需要selectAs方法
//                            .select(Competition::getCId,Competition::getCName,Competition::getCRule,Competition::getCRule,Competition::getPlace,Competition::getTimeNeed)
//                            //指名Judge表里面的Name字段对应结果展示类的HeadJudgeName字段
//                            .selectAs(Judge::getName,ShowCompetitionInfo::getHeadJudgeName)
//                            //连表查询条件，两个参数相等
//                            .leftJoin(Judge.class,Judge::getJId,Competition::getHeadJudge)
//                            //查询条件
//                            .likeRight(Competition::getCId,code));   //likeRight：'值%'
//        }
//
//        if(size==0) code+="_";
//        else if(size==1) code+="1";
//        else if(size==2) code+="2";
//        else code+="0";
//
//        return competitionMapper.selectJoinList(ShowCompetitionInfo.class,
//                new MPJLambdaWrapper<Competition>()
//                        .select(Competition::getCId,Competition::getCName,Competition::getCRule,Competition::getCRule,Competition::getPlace,Competition::getTimeNeed)
//                        .selectAs(Judge::getName,ShowCompetitionInfo::getHeadJudgeName)
//                        .leftJoin(Judge.class,Judge::getJId,Competition::getHeadJudge)
//                        .likeRight(Competition::getCId,code));
//    }



    @ApiOperation(value = "获取比赛编排结果")
    @GetMapping("/arrange/getArrangeResult")
    public RespBean getArrangeResult(){
        List<Competition> arrangeResult = competitionService.getArrangeResult();
        return RespBean.success("获取成功",arrangeResult);
    }
}
